package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 31803
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-06-20 15:27:20
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{
    @Autowired
    TbTypeMapper tbTypeMapper;
    @Autowired
    HttpServletRequest request;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public R TypetreeList() {

        List<TbType> tbTypes = tbTypeMapper.selectList(null);
        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setName(type.getTypeName());
            node.setParentId(type.getTypePid().toString());
            node.putExtra("typeImage", type.getTypeImage());
            node.putExtra("typeTop", type.getTypeTop());
            node.putExtra("typeStatus", type.getTypeStatus());
        });
        return R.ok().setData(treeList);
    }

    @Override
    public R addType(TbType type) {
        //查看名称是否重复
        TbType tbType = tbTypeMapper.selectOne(new QueryWrapper<TbType>().lambda().eq(TbType::getTypeName, type.getTypeName()));
        if(tbType!=null){
            return R.erorr().setMsg("用户名称已经存在");
        }
        //添加类型
        String token = request.getHeader("token");
        if(token==null){
            return R.erorr().setMsg("用户未登录");
        }
        String userName = TokenUtils.getUserName(token);
        type.setCreateBy(userName);
        type.setCreateTime(new Date());
        tbTypeMapper.insert(type);
        return R.ok();
    }

    @Override
    public R uploadImage(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.erorr().setMsg("上传失败");
    }

    @Override
    public R updateType(TbType type) {

        //修改
        String token = request.getHeader("token");
        //维护字段
        if(token==null){
            return R.erorr().setMsg("用户未登录");
        }
        String userName = TokenUtils.getUserName(token);
        type.setUpdateBy(userName);
        type.setUpdateTime(new Date());
        tbTypeMapper.updateById(type);

        return R.ok();
    }

    @Override
    public R SelecttreeList() {

        List<TbType> tbTypes = tbTypeMapper.selectList(null);
        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, node) -> {
            node.setId(type.getTypeId().toString());
            node.setName(type.getTypeName());
            node.setParentId(type.getTypePid().toString());
            node.putExtra("label", type.getTypeName());
            node.putExtra("value", type.getTypeId());
        });
        return R.ok().setData(treeList);
    }
}




